clear      
clear mata
clear matrix
version 11.0                                                        
set more off
set memory 1000m
capture log close
set scheme s2color
# delimit ; 
*==================================================================================================; 
* File-Name:       	IDACC_replicationFile_mainFiguresTablesOnly.do                                 		
* Date:            	February 27, 2014 
* Author:          	DD                                       
* Purpose:         	Analysis, tables, and figures for IDACC paper
* Data Used:       	IDACC_data.dta
*==================================================================================================; 
* Variables:
* treatment: 1 Baseline, 2 Identity,
* ingroup: 0 Out-group, 1 In-group, 2 Baseline
* electiondecision: 0 not retained, 1 retained
* choice 1: 5-20 investment into effort pre-election, acronym: c1
* choice 2: 5-20 investment into effort post-election, acronym: c2
* type: 20-50 level of competence, acronym: tv
* noise: -15 - +15 random draw from uniform distribution
* type_omega: approx. 5-65, acronmym: extv
* period: 1-20 round of play
* firsthalf: 0 Second half, 1 First half of session
*
* Generated by this do-file:
* bias: -1 Out-group bias, 1 In-group bias
*==================================================================================================;
*==================================================================================================; 
* I. Define macros and programs
*==================================================================================================; 
* set path to subdirectories;
local data "data";
local output "figures";

* define locals;
local graphr "graphr(fc(white) lc(white) ifc(white) ilc(white))";
local grcom "ycommon xcommon imargin(small)";
local stdl "lp(solid) lc(black) lw(thick)";
local stdl_spike "lp(solid) lc(gs12) lw(thick)";
local pointer "msi(zero) mcol(white) mlabp(0) mlabc(black) mlabs(medlarge)";
local area = 976;

*==================================================================================================;
*==================================================================================================; 
* II. Analysis  
*==================================================================================================; 
* 5. Aggregate patterns
*==================================================================================================; 
* 5.1 Retention decision (electiondecision)
*==================================================================================================; 
* Figure 1: Effect of type_omega and/or choice 1 on electiondecision
*==================================================================================================; 
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==voter);  

******************************************************************************;
* Generate qoi;
g lowc1 = 0;  replace lowc1 = 1 if(c1<13); g x = .; g mean = .; g lb = .; g ub = .; 
local k = 1;
foreach i in 2 1 0 {;
	foreach j in 1 0 {;
		bs qoi = (r(mu_1)-r(mu_2)), reps(100) seed(01010) cl(session) l(90):
		ttest election if(ingroup==`i'&lowc1==`j'), by(exlowtype); 
		mat ci = e(ci_percentile); replace lb = ci[1,1] in `k'; replace ub = ci[2,1] in `k'; 
		mat qoi = e(b); replace mean = qoi[1,1] in `k'; replace x = `k' in `k'; local k = `k' + 1;
		};
	local k = `k' + 1;	
	};
local k = 9;
foreach i in 2 1 0 {;
	foreach j in 1 0 {;
		bs qoi = (r(mu_1)-r(mu_2)), reps(100) seed(01010) cl(session) l(90):
		ttest election if(ingroup==`i'&exlowtype==`j'), by(lowc1); 
		mat ci = e(ci_percentile); replace lb = ci[1,1] in `k'; replace ub = ci[2,1] in `k'; 
		mat qoi = e(b); replace mean = qoi[1,1] in `k'; replace x = `k' in `k';
		local k = `k' + 1;
		};
	local k = `k' + 1;
	};

******************************************************************************;
* Plot qoi;
gr tw 
	(bar mean x if(x==1|x==2), col(gray) barw(.9))
	(bar mean x if(x==4|x==5), col(black) barw(.9))
	(bar mean x if(x==7|x==8), lc(black) fc(white) lw(medium) barw(.9))
	(rcap lb ub x if(x<=8), lc(gs12) lw(thick)),
	ysc(range(-.05 .6)) ylab(0(.1).6) xsc(off) leg(off) `graphr' yti(" ") 
	yline(0, lc(black) lp(solid)); 
gr export `output'/bar_retention_bytypec1.pdf, replace;
gr export `output'/bar_retention_bytypec1.eps, replace; 

gr tw 
	(bar mean x if(x==9|x==10), col(gray) barw(.9))
	(bar mean x if(x==12|x==13), col(black) barw(.9))
	(bar mean x if(x==15|x==16), lc(black) fc(white) lw(medium) barw(.9))
	(rcap lb ub x if(x>=9&x<=16), lc(gs12) lw(thick)),
	ysc(alt range(-.05 .6)) ylab(0(.1).6) xsc(off) leg(off) `graphr' yti(" ")
	yline(0, lc(black) lp(solid)); 
gr export `output'/bar_retention_byc1type.pdf, replace;
gr export `output'/bar_retention_byc1type.eps, replace;

*=================================================================================================; 
* Figure 2: Expected values of choice 1 over type and ingroup
*=================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if(((treatment==baseline|treatment==identity)&period>0)&playerid==representative);

******************************************************************************;
* Generate qoi;
g y = .;  g lb = .; g ub = .;
forval i = 0/2 {;
	estsimp reg c1 tv if(ingroup==`i');
	forval j = 20/50 {;
		setx tv `j'; 
		simqi, genev(y0);
		sum y0; local mean = r(mean);
		replace y = `mean' if(ingroup==`i'&tv==`j');
		drop y0;
	};
	drop b*;
};
	
g tvpl = tv+.25;
g tvplpl = tv+.5;

******************************************************************************;
* Plot qoi;
gr tw 
(lowess c1 tv if(ingroup==1), lc(black) lw(vthick))
(lowess c1 tv if(ingroup==2), lc(gray) lw(vthick))
(lowess c1 tv if(ingroup==0), lc(black) lw(vthick) lp(dash))
(scatter y tv if(ingroup==1), mc(black) msiz(large))
(scatter y tv if(ingroup==2), mc(gray) msiz(large))
(scatter y tv if(ingroup==0), mlc(black) mfc(white) msiz(large)),
`graphr' leg(off) ysc(range(10 15)) ylab(10(1)15) yti("") xti("")
ysize(7);
gr export `output'/scatter_ev_c1.eps, replace; 
gr export `output'/scatter_ev_c1.pdf, replace;

*=================================================================================================;
* 6.1.2 What drives the behavioral group biases?
*=================================================================================================;
* Manifestations of behavioral bias
*=================================================================================================;
* Figure 3: Proportion retained for in-group and out-group biased voters
*=================================================================================================;
* Load data; 
use `data'/LandaDuell_data_bias, clear; 

* Get bias measure obtained for voters and transfer to subjects who are representatives in the first
* half of the session; 
vallist sessub; 
foreach s in `r(list)' {;
	sum bias if(sessub==`s'); local bias`s' = r(mean);
	};

use `data'/landaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==voter);

g bias = .; vallist sessub if(treatment==identity); 
foreach s in `r(list)' {;
	replace bias = `bias`s'' if(sessub==`s');
	};

g inbias = 0 if(bias==-1); replace inbias = 1 if(bias==1); replace inbias = 2 if(bias==.); 
******************************************************************************;
* Generate qoi;
g lowc1 = 0; replace lowc1 = 1 if(c1<13); g x = .; g mean = .; g lb = .; g ub = .; local m = 1;
foreach i in 2 1 0 {; 
	if(`i'==2){;
	foreach j in 1 0 {; 
		foreach k in 1 0 {; 
				bs qoi = r(mean), reps(100) seed(01010) l(90) cl(session): sum election 
					if(inbias==`i'&exlowtype==`j'&lowc1==`k'); 
				mat ci = e(ci_percentile); replace lb = ci[1,1] in `m'; replace ub = ci[2,1] in `m'; 
				mat qoi = e(b); replace mean = qoi[1,1] in `m'; replace x = `m'  in `m'; 
				local m = `m' + 1;
				}; 
			};
		};
	if(`i'<2){;
		foreach j in 1 0 {; 
			foreach k in 1 0 {;
				foreach l in 1 0 {;
					bs qoi = r(mean), reps(100) seed(01010) l(90) cl(session): sum election 
						if(inbias==`i'&ingroup==`j'&exlowtype==`k'&lowc1==`l'); 
					mat ci = e(ci_percentile); replace lb = ci[1,1] in `m'; replace ub = ci[2,1] in `m'; 
					mat qoi = e(b); replace mean = qoi[1,1] in `m'; replace x = `m'  in `m'; 
					local m = `m' + 1;
					}; 
				};
			};
		};	
	};
	
replace x = x+1 if(x>=3); replace x = x+3 if(x>=6); 
replace x = x+1 if(x>=11);  replace x = x+2 if(x>=14);
replace x = x+1 if(x>=18);  replace x = x+3 if(x>=21);
replace x = x+1 if(x>=26);  replace x = x+2 if(x>=29); 
replace x = x+1 if(x>=33);  

gr tw 
	(bar mean x if(x==1|x==2|x==9|x==10|x==16|x==17|x==24|x==25|x==31|x==32), 
		lc(black) fc(white) lw(thick) barw(.9) yaxis(1 2))
	(bar mean x if(x==4|x==5|x==12|x==13|x==19|x==20|x==27|x==28|x==34|x==35), 
		lc(black) fc(gs4) lw(thick) barw(.9))
	(rcap lb ub x, lc(gs12) lw(thick)),
	ylab(0(.2)1) xlab(1(5)35) xsc(off) leg(off) `graphr' yti("") 
	name(baseline, replace) xsiz(10);

gr export `output'/sum_stats_electiondection_bias.eps, replace; 	
gr export `output'/sum_stats_electiondection_bias.pdf, replace; 	

*==================================================================================================;
* Appendix: Robustness Checks
*==================================================================================================;
* A. Summary statistics
*==================================================================================================;
* Table A.1 Number of subjects, Klees, and Kandinskys by session
*==================================================================================================;
* Load data; 
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative);

* Baseline;
unique sessub if(session==7);
unique sessub if(session==8);
unique sessub if(session==10);

* Identity;
unique sessub if(session==33); 
unique sessub if(session==33&grouptype==klee); 
unique sessub if(session==33&grouptype==kandinsky); 
unique sessub if(session==34);
unique sessub if(session==34&grouptype==klee); 
unique sessub if(session==34&grouptype==kandinsky); 
unique sessub if(session==35);
unique sessub if(session==35&grouptype==klee); 
unique sessub if(session==35&grouptype==kandinsky); 
unique sessub if(session==36); 
unique sessub if(session==36&grouptype==klee); 
unique sessub if(session==36&grouptype==kandinsky); 

*==================================================================================================;
* Table A.2 Summary statistics of main variables by treatment
*==================================================================================================;
* Baseline;
sum electiondecision tv extv c1 if(treatment==baseline);
sum electiondecision c2 if(treatment==baseline&c2>0);

* Identity
sum electiondecision tv extv c1 if(treatment==identity);
sum electiondecision tv extv c1 if(treatment==identity&ingroup==1);
sum electiondecision tv extv c1 if(treatment==identity&ingroup==0);
sum c2 if(treatment==identity&c2>0);
sum c2 if(treatment==identity&ingroup==1&c2>0);
sum c2 if(treatment==identity&ingroup==0&c2>0);

*==================================================================================================;
* Robustness of the choice 1 - model
*==================================================================================================;
* Table B: Regression of choice 1 on type and treatment dummies
*==================================================================================================;
* Load data;
use `data'/LandaDuell_data, clear; 
keep if((treatment==baseline|treatment==identity)&period>0&playerid==representative); 

******************************************************************************;
* Generate qoi;	
g outgroup = 0; replace outgroup = 1 if(ingroup==0);
replace baseline = 0 if(baseline==.); 
g retention = election;

forval i = 2/10 {;
	g round`i' = 0; replace round`i' = 1 if(round==`i');
};

xtset sessub round;
foreach v in tv baseline outgroup retention tempprofit {;
	g l`v' = l.`v'; 
	};

* Models;
* Model 1;
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup; mat V = e(V); 
g bingroup = _b[tv] in 1; g seingroup = sqrt(V[1,1]) in 1;
margins, dydx(tv) subpop(baseline) post; mat V = e(V); mat b = e(b); 
g bbaseline = b[1,1] in 1; g sebaseline = sqrt(V[1,1]) in 1; 
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup;
qui margins, dydx(tv) subpop(outgroup) post; mat V = e(V); mat b = e(b); 
g boutgroup = b[1,1] in 1; g seoutgroup = sqrt(V[1,1]) in 1; 

* Model 2;
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, vce(cl sessub); mat V = e(V); 
replace bingroup = _b[tv] in 2; replace seingroup = sqrt(V[1,1]) in 2; 
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace bbaseline = b[1,1] in 2; replace sebaseline = sqrt(V[1,1]) in 2; 
reg c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, vce(cl sessub);
margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace boutgroup = b[1,1] in 2; replace seoutgroup = sqrt(V[1,1]) in 2;

* Model 3;
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, ll(5) ul(20) vce(cl sessub); 
mat V = e(V); 
replace bingroup =  _b[tv] in 3; replace seingroup = sqrt(V[1,1]) in 3;
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace bbaseline = b[1,1] in 3; replace sebaseline = sqrt(V[1,1]) in 3; 
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup, ll(5) ul(20) vce(cl sessub);
margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace boutgroup = b[1,1] in 3; replace seoutgroup = sqrt(V[1,1]) in 3;

* Model 4;
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, ll(5) ul(20) 
vce(cl sessub); mat V = e(V); 
replace bingroup =  _b[tv] in 4; replace seingroup = sqrt(V[1,1]) in 4;
margins, dydx(tv) subpop(baseline) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace bbaseline = b[1,1] in 4; replace sebaseline = sqrt(V[1,1]) in 4; 
tobit c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, ll(5) ul(20) 
vce(cl sessub);
margins, dydx(tv) subpop(outgroup) vce(unconditional) post; mat V = e(V); mat b = e(b); 
replace boutgroup =  b[1,1] in 4; replace seoutgroup = sqrt(V[1,1]) in 4;

* Model 5;
xtreg c1 tv i.round if(ingroup==1), fe vce(cl sessub); mat b = e(b); mat V = e(V); 
replace bingroup = b[1,1] in 5; replace seingroup = sqrt(V[1,1]) in 5;
xtreg c1 tv i.round if(baseline==1), fe vce(cl sessub); mat b = e(b); mat V = e(V); 
replace bbaseline = b[1,1] in 5; replace sebaseline = sqrt(V[1,1]) in 5;
xtreg c1 tv i.round if(outgroup==1), fe vce(cl sessub); mat b = e(b); mat V = e(V); 
replace boutgroup =  b[1,1] in 5; replace seoutgroup = sqrt(V[1,1]) in 5;

* Model 6;
xtreg c.c1 c.tv i.baseline i.outgroup c.tv#i.baseline c.tv#i.outgroup i.round, re vce(robust);
mat V = e(V); 
replace bingroup =  _b[tv] in 6; replace seingroup = sqrt(V[1,1]) in 6; 
margins, dydx(tv) subpop(baseline) post; mat V = e(V); mat b = e(b); 
replace bbaseline = b[1,1] in 6; replace sebaseline = sqrt(V[1,1]) in 6; 
xtreg c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, re vce(robust);
margins, dydx(tv) subpop(outgroup) post; mat V = e(V); mat b = e(b); 
replace boutgroup = b[1,1] in 6; replace seoutgroup = sqrt(V[1,1]) in 6;

* Model 7;
xttobit c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, ll(5) ul(20) vce(boot); 
mat V = e(V); 
replace bingroup =  _b[tv] in 7; replace seingroup = sqrt(V[1,1]) in 7; 
margins, dydx(tv) subpop(baseline) post; mat V = e(V); mat b = e(b); 
replace bbaseline = b[1,1] in 7; replace sebaseline = sqrt(V[1,1]) in 7; 
xttobit c1 tv baseline outgroup c.tv#i.baseline c.tv#i.outgroup i.round, ll(5) ul(20) vce(boot);
margins, dydx(tv) subpop(outgroup) post; mat V = e(V); mat b = e(b); 
replace boutgroup =  b[1,1] in 7; replace seoutgroup = sqrt(V[1,1]) in 7;*/

* Model 8;
xtabond c1 tv round* if(ingroup==1), twostep lag(1) maxldep(1) maxlags(2) artest(3) vce(robust) 
pre(ltv lbaseline loutgroup) endogenous(lretention ltempprofit); 
replace bingroup = _b[tv] in 8; replace seingroup = _se[tv] in 8; 

xtabond c1 tv round* if(ingroup==2), twostep lag(1) maxldep(1) maxlags(2) artest(3) vce(robust)
pre(ltv lbaseline loutgroup) endogenous(lretention ltempprofit); 
replace bbaseline = _b[tv] in 8; replace sebaseline = _se[tv] in 8; 

xtabond c1 tv round* if(ingroup==1), twostep lag(1) maxldep(1) maxlags(2) artest(3) vce(robust) 
pre(ltv lbaseline loutgroup) endogenous(lretention ltempprofit); 
replace boutgroup = _b[tv] in 8; replace seoutgroup = _se[tv] in 8;

mkmat bbaseline sebaseline bingroup seingroup boutgroup seoutgroup if(bbaseline~=.), mat(results);
mat list results;
mat results = results';

mat rown results = "Baseline" "se" "In-Group" "se" "Out-Group" "se";
mat coln results = "1" "2" "3" "4" "5" "6" "7" "8";
outtable using `output'/choice1Model_robustness, mat(results) nobox f(%6.4g) replace center;

*==================================================================================================;
*==================================================================================================;
* III. Exit
*==================================================================================================;
exit;
*==================================================================================================;
*==================================================================================================;
*==================================================================================================;

